package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import com.itheima.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @outhor
 * @date 2020/1/13 12:11
 */

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 图片上传七牛网
     *
     * @param imgFile
     * @return
     */
    @RequestMapping("upload")
    public Result upload(MultipartFile imgFile) {
        try {
            //1.修改文件名称
            //获取string类型的文件名称
            String oldFilename = imgFile.getOriginalFilename();
            //字符串中最后一个"."的位置数
            int indexOf = oldFilename.lastIndexOf(".");
            //以字符串中最后一个"."的位置数为开始向后截取字符串(截取后缀名如".jpg")
            String suffix = oldFilename.substring(indexOf);
            //使用UUID随机名字并拼接后缀名
            String newFilename = UUID.randomUUID().toString() + suffix;
            //2.调用七牛工具类上传文件
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), newFilename);
            //3.上传完成后将上传的文件名保存到Redis中的TOTAL_UPLOAD_IMGFILE_NAME(所有上传图片中)以便定时任务中对比清理脏数据
            redisUtil.sadd("TOTAL_UPLOAD_IMGFILE_NAME", newFilename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, newFilename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal) {
        try {
            setmealService.add(setmeal);
            //保存数据库后后将保存的文件名保存到Redis中的SAVE_ADD_IMGFILE_NAME(保存数据库图片中)以便定时任务中对比清理脏数据
            redisUtil.sadd("SAVE_ADD_IMGFILE_NAME", setmeal.getImg());
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = setmealService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("delete")
    public Result delete(Integer id,String img) {
        try {
            setmealService.delete(id,img);
            //删除redis两个集合中的数据
            redisUtil.srem("TOTAL_UPLOAD_IMGFILE_NAME",img);
            redisUtil.srem("SAVE_ADD_IMGFILE_NAME",img);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        try {
            Map map = setmealService.findById(id);
            return new Result(true, "回显数据查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "回显数据查询失败");
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Setmeal setmeal) {
        try {
            setmealService.edit(setmeal);
            if (setmeal.getOldImgName() != null  && (!setmeal.getOldImgName().equals(setmeal.getImg()))) {
                //保存数据库后后将保存的文件名保存到Redis中的SAVE_ADD_IMGFILE_NAME(保存数据库图片中)以便定时任务中对比清理脏数据
                redisUtil.sadd("SAVE_ADD_IMGFILE_NAME", setmeal.getImg());
                //删除redis两个集合中的数据
                redisUtil.srem("TOTAL_UPLOAD_IMGFILE_NAME",setmeal.getOldImgName());
                redisUtil.srem("SAVE_ADD_IMGFILE_NAME",setmeal.getOldImgName());
            }
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }
}
